package ControllerLayer;
import DomainLayer.*;
import java.util.ArrayList;

public class LoanController
{
    private ToolStock stock;
    private CustomerController customers;
    private UserCollection users;
    private LoanCollection loanCollection;
    private ArrayList<Tool> toolLoanedList;
    
    public LoanController()
    {
        stock= ToolStock.getInstance();
        customers=CustomerController.getInstance();
        users= UserCollection.getInstance();
        loanCollection=LoanCollection.getInstance();
        resetToolLoanedList();
    }
    
    public void prepareLoan(String barCode)
    {
        Tool tool=searchAvailableTool(barCode);
        if(tool!=null)
        {
            addToolToToolLoanedList(tool);
        }
        else
        {
            throw new NullPointerException();
        }
    }
   
    
    public int createLoan(int customerID, String assistantID,int year, int month, int day)
    {
        ArrayList<Tool> list= getToolLoanedList();
        
            Customer customer=customers.searchCustomer(customerID);
        if(customer==null)
        {
            throw new NullPointerException();
        }
        Assistant assistant= (Assistant)users.findUser(assistantID);
        return loanCollection.createLoan(list,customer,assistant,year, month,day);
    }
    
    public Loan searchLoan(int id)
    {
      return   loanCollection.searchLoan(id);
    }
    
    public void deleteLoan(int id)
    {
        unloanAllItemsOnList( loanCollection.searchLoan(id).getLoanList());
        loanCollection.deleteLoan(id);
        
    }
    
     public void resetToolLoanedList()
    {
        toolLoanedList=new ArrayList<Tool>();
    }
    
    public Tool searchAvailableTool(String barCode)
    {
        return stock.searchAvailableTool(stock.searchTools(barCode));
    }
    
    public void loanTool(Tool tool)
    {
        stock.loanTool(tool);
    }
    
    public void unloanTool(Tool tool)
    {
        stock.unloanTool(tool);
    }
    
    public void addToolToToolLoanedList(Tool tool)
    {
        toolLoanedList.add(tool);
        loanTool(tool);
    }
    
    public void unloanAllItemsOnList(ArrayList<Tool> list)
    {
        for(Tool tool: list)
        {
            unloanTool(tool);
        }
        
        
    }
    
    public ArrayList<Tool> getToolLoanedList()
    {
        return toolLoanedList;
    }
        
    public String printLoan(int id)
    {
        return loanCollection.printLoan(id);
    }
    
    /**
     * @param id a unique number of loan.
     * @param lastYear, lastMonth, lastDay - the end date of loan.
     * @return String an information about the loan according days of loan.
     */
    public String printInvoiceLoan(int id,int lastYear, int lastMonth, int lastDay)
    {Loan loan= searchLoan(id);
        int year=loan.getYear();
        int month= loan.getMonth();
        int day= loan.getDay();
        loan.setLastYear(lastYear);
        loan.setLastMonth(lastMonth);
        loan.setLastDay(lastDay);
        DateDifference dateDifference= new DateDifference(year,month,day,lastYear,lastMonth,lastDay);
        int days= dateDifference.getDays();
        return loan.createInvoice(days);
    }
}